package com.pdg.islandinterloper;

import java.util.HashMap;
import java.util.Map;

import com.pdg.islandinterloper.clientstatehandlers.AboutClientStateHandler;
import com.pdg.islandinterloper.clientstatehandlers.CreateWorldClientStateHandler;
import com.pdg.islandinterloper.clientstatehandlers.InstructionsClientStateHandler;
import com.pdg.islandinterloper.clientstatehandlers.MainClientStateHandler;
import com.pdg.islandinterloper.clientstatehandlers.NewProfileClientStateHandler;
import com.pdg.islandinterloper.clientstatehandlers.OpenProfileClientStateHandler;
import com.pdg.islandinterloper.clientstatehandlers.PlayClientStateHandler;
import com.pdg.islandinterloper.clientstatehandlers.SettingsClientStateHandler;
import com.pdg.islandinterloper.clientstatehandlers.StatisticsClientStateHandler;
import com.pdg.islandinterloper.clientstatehandlers.UninitializeProfileClientStateHandler;
import com.pdg.islandinterloper.database.IslandInterloperDatabase;
import com.pdg.islandinterloper.enums.ClientState;
import com.pdg.islandinterloper.interfaces.ClientStateHandler;

import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {
	
	private static final ClientState DEFAULT_CLIENT_STATE = ClientState.MAIN;
	private static final long DEFAULT_PROFILE_ID = 0;
	private static final String PREFERENCE_CLIENT_STATE = "client-state";
	private static final String PREFERENCE_PROFILE_ID = "profile-id";
	private ClientState state = null;
	private long profileId = DEFAULT_PROFILE_ID;
	private Map<ClientState,ClientStateHandler<MainActivity>> handlers = new HashMap<ClientState,ClientStateHandler<MainActivity>>();
	private IslandInterloperDatabase database;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        database = new IslandInterloperDatabase(this);
        createClientStateHandlers();
    }
    
    public IslandInterloperDatabase getDatabase(){
    	return database;
    }
    
    @Override
    public void onStart(){
    	super.onStart();
    	loadClientStateFromPreferences();
    }
    
	private void loadClientStateFromPreferences() {
    	SharedPreferences thePreferences = getPreferences(MODE_PRIVATE);
    	ClientState theClientState = ClientState.valueOf(thePreferences.getString(PREFERENCE_CLIENT_STATE, DEFAULT_CLIENT_STATE.toString()));
    	setProfileId(thePreferences.getLong(PREFERENCE_PROFILE_ID, DEFAULT_PROFILE_ID));
    	setState(theClientState);//DO THIS LAST!!!
	}

	@Override
    public void onStop(){
    	super.onStop();
    	saveClientStateToPrefrences();
    }
    
    private void saveClientStateToPrefrences() {
    	Editor theEditor = getPreferences(MODE_PRIVATE).edit();
    	theEditor.putString(PREFERENCE_CLIENT_STATE, getState().toString());
    	theEditor.putLong(PREFERENCE_PROFILE_ID,getProfileId());
    	theEditor.commit();
	}

	@Override
    public void onDestroy(){
    	super.onDestroy();
    }
    
    @Override
    public void onPause(){
    	super.onPause();
    	hideCurrentClientState();
    }
    
    @Override
    public void onResume(){
    	super.onResume();
    	showCurrentClientState();
    }
    
    @Override
    public void onRestart(){
    	super.onRestart();
    }
    
    

    private void createClientStateHandlers() {
    	handlers.put(ClientState.MAIN, new MainClientStateHandler(this));
    	handlers.put(ClientState.ABOUT, new AboutClientStateHandler(this));
    	handlers.put(ClientState.INSTRUCTIONS, new InstructionsClientStateHandler(this));
    	handlers.put(ClientState.SETTINGS, new SettingsClientStateHandler(this));
    	handlers.put(ClientState.STATISTICS, new StatisticsClientStateHandler(this));
    	handlers.put(ClientState.PLAY, new PlayClientStateHandler(this));
    	handlers.put(ClientState.NEW_PROFILE, new NewProfileClientStateHandler(this));
    	handlers.put(ClientState.OPEN_PROFILE, new OpenProfileClientStateHandler(this));
    	handlers.put(ClientState.UNINITIALIZED_PROFILE, new UninitializeProfileClientStateHandler(this));
    	handlers.put(ClientState.CREATE_WORLD, new CreateWorldClientStateHandler(this));
	}

	@Override
    public boolean onPrepareOptionsMenu(Menu theMenu) {
		theMenu.clear();
        getMenuInflater().inflate(getState().getMenu(), theMenu);
        return true;
    }
    
    public void handleButtonClick(View theView){
    	ClientStateHandler<MainActivity> theHandler = getCurrentClientStateHandler();
    	theHandler.handleButtonClick(theView);
    }

	private ClientStateHandler<MainActivity> getCurrentClientStateHandler() {
		return handlers.get(getState());
	}

	public ClientState getState() {
		return state;
	}

	public void setState(ClientState theState) {
		hideCurrentClientState();
		state = theState;
		showCurrentClientState();
	}
	
	private void showCurrentClientState() {
		setContentView(getState().getLayout());
		if(getCurrentClientStateHandler()!=null){
			getCurrentClientStateHandler().handleShow();
		}
	}

	private void hideCurrentClientState() {
		if(getCurrentClientStateHandler()!=null){
			getCurrentClientStateHandler().handleHide();
		}
	}

	public boolean  onOptionsItemSelected (MenuItem theItem){
    	ClientStateHandler<MainActivity> theHandler = getCurrentClientStateHandler();
    	return theHandler.handleMenu(theItem);
	}

	public long getProfileId() {
		return profileId;
	}

	public void setProfileId(long profileId) {
		this.profileId = profileId;
	}
}
